//
//  StatusCellTopView.swift
//  SinaWeibo
//
//  Created by mac on 15/12/23.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class StatusCellTopView: UIView {
    //定义 视图的底部约束的属性
    var bottomConstraints: Constraint?
    //定义微博模型属性
    var status:Status? {
    didSet {
      //绑定数据
        iconImage.sd_setImageWithURL(status?.user?.headURL)
        nameLable.text = status?.user?.name
        mbrankImage.image = status?.user?.mbrankImage
        verifid_type_image.image = status?.user?.verified_type_image
        //TODO:后续完成
     contentLable.text = status?.text
        
        pictureView.imageURLs = status?.imageURLs
        
        
        //在更新约束之前  将之前的约束关系 先卸载掉底部的 约束 
        self.bottomConstraints?.uninstall()
        //需要根据是 否有配图 来动态的调整约束关系 
        // where cell 的服用产生 约束关系的多余 
        if let  urls = status?.imageURLs where urls.count > 0 {
            //配图视图 再一个{}内 对应的闭包无法简写
            self.snp_makeConstraints(closure: { (make) -> Void in
                self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatuCellMarigin).constraint
            })
        }else{
            //没有配图  
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.bottomConstraints = make.bottom.equalTo(contentLable.snp_bottom).offset(StatuCellMarigin).constraint
            })
        }
        
    }
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置背景颜色  调试用
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        addSubview(iconImage)
        addSubview(nameLable)
        addSubview(mbrankImage)
        addSubview(verifid_type_image)
        addSubview(timaLable)
        addSubview(sourceLable)
        addSubview(contentLable)
        addSubview(pictureView)
        addSubview(sepView)
        iconImage.layer.cornerRadius = 18
        iconImage.clipsToBounds = true
        sepView.backgroundColor = UIColor.grayColor()
        
        //设置约束
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.height.equalTo(8)
        }
        
        
        
        //设置约束
        iconImage.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(self.snp_left).offset(StatuCellMarigin)
            make.top.equalTo(sepView.snp_top).offset(StatuCellMarigin)
            
            make.size.equalTo(CGSize(width: StatusCellHeadImageWeight, height: StatusCellHeadImageWeight))
 
        }
        nameLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImage.snp_right).offset(StatuCellMarigin)
            make.top.equalTo(iconImage.snp_top)
        }
        
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLable.snp_top)
            make.left.equalTo(nameLable.snp_right).offset(StatuCellMarigin)
        }

            
        verifid_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right).offset(-4)
            make.centerY.equalTo(iconImage.snp_bottom).offset(-4)
        }
        timaLable.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(iconImage.snp_right).offset(StatuCellMarigin)
        }
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timaLable.snp_bottom)
            make.left.equalTo(timaLable.snp_right).offset(StatuCellMarigin)
        }
        
        
        contentLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(StatuCellMarigin)
            make.left.equalTo(self.snp_left).offset(StatuCellMarigin)
          //  make.right.equalTo(self.snp_right).offset(-StatuCellMarigin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLable.snp_bottom).offset(StatuCellMarigin)
            make.left.equalTo(contentLable.snp_left)
          //  make.size.equalTo(CGSize(width: 100, height: 100))
        }

        

        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatuCellMarigin).constraint
            
        }
        
        
        
    }
    //懒加载所有子视图 
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLable:UILabel = UILabel(title: "林可可", color: UIColor.orangeColor(), fontSize: 14)
    private lazy var mbrankImage:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var verifid_type_image:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var timaLable:UILabel = UILabel(title: "00:00", color: UIColor.orangeColor(), fontSize: 10)
    private lazy var sourceLable:UILabel = UILabel(title: "玉林专用定制客户端", color:  UIColor.orangeColor(), fontSize: 10)
    private lazy var contentLable:UILabel = UILabel(title: "晚上十二点后睡觉等于慢性自杀，不吃早餐等于慢性自杀，经常烧烤等于慢性自杀，手机24小时开机等于慢性自杀，长期呆室内等于慢性自杀，过多抱怨等于慢性自杀，缺乏锻炼等于慢性自杀。我突然发现，我一天啥都没干，光TM自杀了！！", color:  UIColor.blackColor(), fontSize: 14,margin:StatuCellMarigin)
    //配图视图懒加载 
    private lazy var pictureView:StatusPictureView = StatusPictureView()

    //懒加载视图分割线 
    private lazy var sepView: UIView = UIView()
}
